//
//  ZJMapVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/21.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJMapVC.h"
#import "Myanotation.h"
#import "locationGPS.h"
#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>
@interface ZJMapVC ()<CLLocationManagerDelegate, MKMapViewDelegate>{
    double HBcurrentLng;
    double HBcurrentLat;
}
@property (weak, nonatomic) IBOutlet MKMapView *mapview;
@property (nonatomic, strong) CLLocationManager *clmgr;
@property (nonatomic, strong) NSArray *pointsArray;
@property (nonatomic, strong) NSMutableArray *annotationArr;

@property (nonatomic, strong) NSArray * latArr;
@property (nonatomic, strong) NSArray * lonArr;

@property (strong, nonatomic)  MKCircle *circle;
@property (strong, nonatomic) MKCircleView * circleView;
@property (nonatomic, strong) LocationManager *manager;

@end

@implementation ZJMapVC

- (CLLocationManager *)clmgr
{
    if (!_clmgr) {
        self.clmgr = [[CLLocationManager alloc] init];
    }
    return _clmgr;
}
- (LocationManager *)manager{
    
    if (!_manager) {
        _manager = [[LocationManager alloc] init];
    }
    return _manager;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //设置代理
    _clmgr.delegate=self;
    //是否允许缩放，一般都会让缩放的
    _mapview.zoomEnabled = YES;
    _mapview.scrollEnabled = YES;
  
    // 根据地区获取经纬度
    [self.manager geocode:self.HBAddress success:^(CLPlacemark *pm) {
        
        NSLog(@"红包位置**********************lat+++++ %f  lon+++++++++ %f",pm.location.coordinate.latitude,pm.location.coordinate.longitude);
        HBcurrentLng = pm.location.coordinate.longitude;
        HBcurrentLat = pm.location.coordinate.latitude;
        [self setupMapCircle];
        double a = HBcurrentLat;
        double b = HBcurrentLng;
        
        NSMutableArray * arr = [NSMutableArray new];
        _latArr = @[@(a - 0.001),@(a + 0.001),@(a),@(a + 0.001)];
        _lonArr = @[@(b - 0.001),@(b + 0.001),@(b),@(b)];
        for (int i = 0; i < _latArr.count; i++) {
            Myanotation * firstAnnotation = [[Myanotation alloc]init];
            //            firstAnnotation.img = [UIImage imageNamed:@"gift"];
            firstAnnotation.title = @"我是你爸爸";
            firstAnnotation.subtitle = @"屌炸了";
            double a = [_latArr[i] doubleValue];
            double b = [_lonArr[i] doubleValue];
            firstAnnotation.coordinate = CLLocationCoordinate2DMake(a, b);
            [arr addObject:firstAnnotation];
        }
        _annotationArr = [arr copy];
        [self.mapview addAnnotations:arr];
    } failure:^{}];

    // 跟踪用户当前位置
    self.mapview.userTrackingMode = MKUserTrackingModeFollow;
}

- (void)setupMapCircle
{
    _circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(HBcurrentLat, HBcurrentLng) radius:300];
    [self.mapview addOverlay:_circle];
}

/*构造圆*/
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    
    if ([overlay isKindOfClass:[MKCircle class]])
    {
        MKCircleView* circleView = [[MKCircleView alloc] initWithOverlay:overlay];
        circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
        circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        circleView.lineWidth = 1.0;
        _circleView = circleView;
        
        return circleView;
    }
    
    return nil;
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        MKAnnotationView *annotationView = [[MKAnnotationView alloc]init];
        annotationView.image = [UIImage imageNamed:KUserHeadPhoto];
        
        //        是否允许显示插入视图*********
        annotationView.canShowCallout = YES;
        
        return annotationView;
    }
    if ([annotation isKindOfClass:[Myanotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:reuseIndetifier];
        }
        annotationView.image = [UIImage imageNamed:@"qhb_icon_x"];
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        annotationView.canShowCallout = YES;
        return annotationView;
    }
    return nil;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views{
    
    
    //    获得mapView的Frame
    CGRect visibleRect = [mapView annotationVisibleRect];
    
    for (MKAnnotationView *view in views) {
        
        CGRect endFrame = view.frame;
        CGRect startFrame = endFrame;
        startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
        view.frame = startFrame;
        [UIView beginAnimations:@"drop" context:NULL];
        [UIView setAnimationDuration:1];
        view.frame = endFrame;
        [UIView commitAnimations];
        
        
    }
    
    
}
- (IBAction)closeBtn:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
